package com.day240701.util;

import com.day240701.entity.Student;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class JDBCDemo01 {
    public static void main(String[] args) throws Exception{
        // 加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");

        // 建立连接
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC", "root", "root");

        // 获得执行语句对象
        String sName = "x";
        int age = 30;
//        PreparedStatement ppstmt = conn.prepareStatement("select sno,sname,password,sex,age,birthday from t_student");
        PreparedStatement ppstmt = conn.prepareStatement("select sno,sname,password,sex,age,birthday from t_student where sname like concat('%', ?, '%') and age = ?");
        ppstmt.setString(1, sName);
        ppstmt.setInt(2, age);
        ResultSet rs = ppstmt.executeQuery();

        ArrayList<Student> sList = new ArrayList<>();
        while (rs.next()) {
            Student stu = new Student();
            stu.setSNo(rs.getInt("sno"));
            stu.setSName(rs.getString("sname"));
            stu.setPassword(rs.getString("password"));
            stu.setSex(rs.getString("sex"));
            stu.setAge(rs.getInt("age"));
            stu.setBirthday(rs.getDate("birthday"));

            sList.add(stu);
            System.out.println(stu);
        }



        rs.close();
        ppstmt.close();
        conn.close();

    }
}
